package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Good;
import com.yc.bean.Types;
import com.yc.biz.GoodBiz;
import com.yc.biz.impl.GoodBizImpl;

@WebServlet("/goodServlet")
public class GoodServlet extends HttpServlet {

	private GoodBiz gb = new GoodBizImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		String op = req.getParameter("op");

		if (op.equals("findTypeByTid")) {
			findTypeByTid(req, resp, out);
		}else if(op.equals("findGoodsByTid")){
			findGoodsByTid(req, resp, out);
		}else if(op.equals("findGoodsByGid")){
			findGoodsByGid(req, resp, out);
		}else if(op.equals("findGoodByRand")){
			findGoodByRand(req, resp, out);
		}else if(op.equals("findPeiJian")){
			findPeiJian(req, resp, out);
		}else if(op.equals("findPeiJianD")){
			findPeiJianD(req, resp, out);
		}else if(op.equals("findSome")){
			findSome(req, resp, out);
		}
	}

	//主页对商品的模糊查询
	private void findSome(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		// TODO Auto-generated method stub
		String str=req.getParameter("str");
		List<Good> goods=gb.findSome(str);
		if(goods.isEmpty()){
			out.print(0);
			return;
		}
		out.print(1);
		req.getSession().setAttribute("list", goods);
		//将类别名称存入tyname中
		req.getSession().setAttribute("tyname", str);
	}

	//下面3件热门配件的展示
	private void findPeiJianD(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		List<Map<String, String>> list=gb.findDown();
		if(list.isEmpty()){
			out.print(0);
			return;
		}else{
			Gson gson=new Gson();
			out.print(gson.toJson(list));
		}
	}

	//上面5件热门配件的展示
	private void findPeiJian(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		List<Map<String, String>> list=gb.findUp();
		if(list.isEmpty()){
			out.print(0);
			return;
		}else{
			Gson gson=new Gson();
			out.print(gson.toJson(list));
		}
		
	}

	//随机查询五件手机
	private void findGoodByRand(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		List<Map<String, String>> list=gb.findGoodsByRand();
		if(list.isEmpty()){
			out.print(0);
			return;
		}else{
			Gson gson=new Gson();
			out.print(gson.toJson(list));
		}
	}

	//根据商品的gid获取商品信息
	private void findGoodsByGid(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int gid= Integer.parseInt(req.getParameter("gid"));
		
		Good good=gb.findGoodGid(gid);
		if(good==null){
			out.print(0);
			return;
		}
		out.print(1);
		req.getSession().setAttribute("good", good);
	}

	//根据两个tid查询商品
	private void findGoodsByTid(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int tid1= Integer.parseInt(req.getParameter("tid1"));
		int tid2= Integer.parseInt(req.getParameter("tid2"));
		List<Map<String, String>> list=gb.findGoodsByTid(tid1, tid2);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		Gson gson=new Gson();
		out.print(gson.toJson(list));
	}

	//根据tid查询这个类别的所有商品
	private void findTypeByTid(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int tid= Integer.parseInt(req.getParameter("tid"));
		
		List<Good> list=gb.findTypeByTid(tid);
		Types type=gb.findTnameByTid(tid);
		if(list.isEmpty() || type==null){
			out.print(0);
			return;
		}
		out.print(1);
		//将此类商品的信息存到list中
		req.getSession().setAttribute("list", list);
		//将类别名称存入tyname中
		req.getSession().setAttribute("tyname", type.getTname());
	}


}
